package com.book.service.serviceImpl;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.book.entity.User;
import com.book.mapper.UserMapper;
import com.book.request.GetUserReq;
import com.book.service.UserService;
import com.book.utils.UUIDutil;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService{
    @Override
    public User findUserByUserName(String username) {
        Wrapper<User> con=new Condition();
        con.eq("username",username);
        return selectOne(con);
    }

    @Override
    public Page getUsers(GetUserReq getUserReq) {
        Wrapper<User> con=new Condition();
        con.eq("user_type",getUserReq.getUserType());
        Page page=new Page();
        page.setSize(getUserReq.getSize());
        page.setCurrent(getUserReq.getCurrent());
        page.setRecords(selectList(con));
        return selectPage(page,con);
}

    @Override
    public void add(User user) {
        user.setUId(UUIDutil.getUUID());
        insert(user);
        user.setUserType("common");
    }

    @Override
    public void grantAdmin(String uId) {
        User user=selectById(uId);
        user.setUserType("admin");
        updateById(user);
    }
}
